	***************************************************************************
	*	ANALYSIS
	* 	Prior to running this program run 1.DataPrep to obtain the file workingdata.dta
	***************************************************************************
	clear all
	capture log close
	cd C:\temp\Data&Methods\DataAnalysis
	log using "analysis`c(current_date)'.log", replace
	
	set more 1
	set scheme sol
	set autotabgraphs on  
	
	* Treatments: 	1= NEUTRAL (points= +3 for all)			4 sessions order 2-2-12-12 + 4 sessions reverse order
	*				2= CONVERGE  (points=  + 5,  +3, + 1)		4 sessions order 2-2-12-12  + 4 sessions reverse order
	*				3= DIVERGE (points= +1, +3, + 5)			4 sessions order 2-2-12-12 + 4 sessions reverse order
	*				4= NEUTRAL+ (points= + 5 for all)			4 sessions order 2-2-12-12 + 4 sessions reverse order
	*				5= NEUTRAL-CHAT (points= + 3 for all)	4 sessions order 2-2-12-12

	use workingdata.dta
	macro define data "workingdata.dta, clear"


*	OVERVIEW
	distinct treat ymd ID
	sort treat groupID
	
	by treat: distinct groupID if groupsize==2 & game<5
	by treat: distinct groupID if groupsize==12
	by treat: distinct groupID if groupsize==2 & game==5
	by treat: distinct groupID if groupsize>2 & game==5
	
	by treat: distinct ID

	bysort treat: tab a country if groupsize==12
	bysort treat: tab order game  if period==1 & subject==1
	
	tabstat duration paid payoffCQ1 sex, stat(mean sd min max) col(stat) f(%6.2f) long

	table (treat partnership) (country) , statistic(mean  beta_star)  nformat(%9.2f) nototals
	table (treat) (session) , statistic(mean duration)  statistic(sd duration)  nformat(%9.1f) totals(treat)	
	

*===============================================
*   COOPERATION  in PHASE 1  (1 obs = 1 economy)
*===============================================
use $data 
preserve
	drop if game==5
	gen outcome1=outcome if period==1
	collapse outcome outcome1, by(treatment sessionID groupID groupsize)		

	table (treat) (groupsize) , statistic(mean  outcome)  statistic(sd outcome)   nformat(%9.3f) nototal
	table (treat) (groupsize) , statistic(mean  outcome1)  nformat(%9.2f) nototal
restore


*====================================================================================================
*   COOPERATION  in PHASE 1  (1 obs= 1 session; Two-sided Wilcoxon-Mann Whitney ranksum test with exact statistics)
*====================================================================================================
preserve
	drop if game==5
	gen outcome1=outcome if period==1
	collapse outcome outcome1, by(treatment sessionID groupsize)
		
	local groupsize  "2  12"
	foreach i of local groupsize {
		dis "==       COOPERATION Phase 1:  groupsize =" `i'  "     =="
		dis "***************************************************** "
		ranksum outcome if groupsize==`i' & inlist(treatment, 1,2), by(treatment)
		ranksum outcome if groupsize==`i' & inlist(treatment, 1,3), by(treatment)
		ranksum outcome if groupsize==`i' & inlist(treatment, 1,4), by(treatment)
		ranksum outcome if groupsize==`i' & inlist(treatment, 2,3), by(treatment)
		ranksum outcome if groupsize==`i' & inlist(treatment, 2,4), by(treatment)
		ranksum outcome if groupsize==`i' & inlist(treatment, 3,4), by(treatment)
		ranksum outcome if groupsize==`i' & inlist(treatment, 1,5), by(treatment)
	}

		dis "==       COOP MIXED GROUPS  In period 1   =="
		ranksum outcome1 if inlist(treatment, 1,2), by(treatment)
		ranksum outcome1 if  inlist(treatment, 1,3), by(treatment)
		ranksum outcome1 if  inlist(treatment, 1,4), by(treatment)
		ranksum outcome1 if  inlist(treatment, 2,3), by(treatment)
		ranksum outcome1 if  inlist(treatment, 2,4), by(treatment)
		ranksum outcome1 if inlist(treatment, 3,4), by(treatment)
		ranksum outcome1 if inlist(treatment, 1,5), by(treatment)

restore


*====================================================================================
* 	FIG SUPP. MATERIALS:  dynamic COOPERATION (PHASE 1)  --  1 obs= 1 mixed group in a period
*====================================================================================
use $data
preserve
	graph drop _all
	drop if game==5
	keep if treat<5
		
	collapse outcome, by(treatment sessionID groupID groupsize period)
	collapse outcome (sem) semc=outcome, by(treatment groupsize period)
		
	sum period
	local per_max=r(max)

	generate hi_sem = outcome + semc   
	generate low_sem = outcome - semc
	
	graph twoway 	 (rarea hi_sem low_sem period if treat==1 , vertical  fin(inten30)  lwidth(none))       (scatter outcome period if treat==2 ,  msymbol(o) connect(l ) lpattern(solid)  mc(black) lcolor(black) lw(medium))     (scatter outcome period if treat==3 ,   msymbol(sh) connect(l) lpattern(solid)  mc(black) lcolor(black) lw(medium) )   (scatter outcome period if treat==4 ,   msymbol(th) connect(l ) lpattern(dash)  mc(red) lcolor(red) lw(medium) )   ,                    ytitle("Cooperation Rate", size(medsmall))         yscale(range(0 1))   ylabel(0(.2)1.0, labs(medium) nogrid)    xlabel(1 5 10 15 20 25 `per_max',  labs(medium) nogrid)   xtitle("Rounds", size(medium))   graphregion(color(white)fcolor(white)) plotregion(margin(zero)) legend(order(1 "Neutral"  2 "Converge"  3 "Diverge"  4 "Neutral+")  ring(0) pos(11) rows(1)  size(small) symxsize(*0.5))      xline(18, lp(dash))     scale(1.2)   by(groupsize, note(""))  

	graph export periods.eps, as(eps) preview(off) replace 	

restore
	
	
*===========================================
*	COOPERATION  in PHASE 2 (1 obs= 1 economy)
*===========================================
use $data 
preserve
	drop if game<5
	tab partnership groupsize if period==1
	collapse outcome , by(treatment sessionID groupID partnership)		
	table (partnership) (treat) , statistic(mean  outcome)  statistic(sd outcome)   nformat(%9.3f) nototals
restore

	
*====================================================================================================
*	COOPERATION  in PHASE 2 (1 obs= 1 session; Two-sided Wilcoxon-Mann Whitney ranksum test with exact statistics)
*====================================================================================================
preserve
	keep if game==5
	gen outcome1=outcome if period==1
	collapse outcome outcome1, by(treatment sessionID partnership)
		
	local partnership  "0 1"
	foreach i of local partnership {
		dis "==       COOPERATION  in Phase 2:  Partnership  =" `i'  "     =="
		dis "********************************************** "
		ranksum outcome if partnership==`i' & inlist(treatment, 1,2), by(treatment)
		ranksum outcome if partnership==`i' & inlist(treatment, 1,3), by(treatment)
		ranksum outcome if partnership==`i' & inlist(treatment, 1,4), by(treatment)
		ranksum outcome if partnership==`i' & inlist(treatment, 2,3), by(treatment)
		ranksum outcome if partnership==`i' & inlist(treatment, 2,4), by(treatment)
		ranksum outcome if partnership==`i' & inlist(treatment, 3,4), by(treatment)
		ranksum outcome if partnership==`i' & inlist(treatment, 1,5), by(treatment)
	}

	local partnership  "0 1"
	foreach i of local partnership {
		dis "==       COOPERATION  in Phase 2 in period 1:  Partnership  =" `i'  "     =="
		dis "********************************************** "
		ranksum outcome1 if partnership==`i' & inlist(treatment, 1,2), by(treatment)
		ranksum outcome1 if partnership==`i' & inlist(treatment, 1,3), by(treatment)
		ranksum outcome1 if partnership==`i' & inlist(treatment, 1,4), by(treatment)
		ranksum outcome1 if partnership==`i' & inlist(treatment, 2,3), by(treatment)
		ranksum outcome1 if partnership==`i' & inlist(treatment, 2,4), by(treatment)
		ranksum outcome1 if partnership==`i' & inlist(treatment, 3,4), by(treatment)
		ranksum outcome1 if partnership==`i' & inlist(treatment, 1,5), by(treatment)
	}

restore

	
	
*======================================================================================
*	COOPERATION  in MIXED GROUPS (PHASE 1  & PHASE 2 separately)  	Unit of obs = Economy 
*======================================================================================
use $data 
preserve
	keep if groupsize>2	
	keep if treat <5
	
	macro define controls "sex duration response_t wrong_ans"		
	collapse outcome   $controls, by (treatment sessionID game  groupID order )
	center response_t wrong_ans duration, inplace  standardize 
	gen game2=(game==2 | game==4)

*** GLM regression	
	est clear
	glm  outcome   i.treat    i.game2   i.order  $controls if game<5, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	test 2.treatment=3.treatment
	test 2.treatment=4.treatment
	test 3.treatment=4.treatment
	eststo reg1

	glm  outcome   i.treat    i.order $controls if game==5, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	test 2.treatment=3.treatment
	test 2.treatment=4.treatment
	test 3.treatment=4.treatment
	eststo reg2

	estout * using output0.tex,  replace cells(b(star fmt(3)) se(par fmt(3))) style(tex) label varlabels(1.order "Order 12-12-2-2"  1.game2 "$2^{nd}$ Supergame" duration "Duration" wrong_ans "Incorrect Answers" _cons Constant response_t "Response Time"   1.sex "Male"  sex "Male") collabels(,none) stats(N, labels("N") fmt(0 3)) prefoot(\hline) starlevels(* 0.10 ** 0.05 *** 0.01) posthead(\hline) margin(p) nobaselevel

restore

***== 		NEUTRAL-CHAT vs NEUTRAL		==***
preserve
	keep if inlist(treat,1,5) & order==0
	keep if groupsize>2	
	
	table (treat) (game) , statistic(mean  outcome)  nformat(%9.3f) totals(treat)
	
	table (treat) (game) if game<5, statistic(mean  outcome)  nformat(%9.3f) totals(treat)
		
	macro define controls "sex duration response_t wrong_ans"		
	collapse outcome   $controls, by (treatment sessionID game  groupID)
	center response_t wrong_ans duration, inplace  standardize 
	gen game2=(game==2 | game==4)

*** GLM regression	
	est clear
	glm  outcome   i.treat    i.game2    $controls if game<5, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	eststo reg1

	glm  outcome   i.treat   $controls if game==5, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	eststo reg2

	estout * using output1.tex,  replace cells(b(star fmt(3)) se(par fmt(3))) style(tex) label varlabels(1.order "Order 12-12-2-2"  1.game2 "$2^{nd}$ Supergame" duration "Duration" wrong_ans "Incorrect Answers" _cons Constant response_t "Response Time"   1.sex "Male"  sex "Male") collabels(,none) stats(N, labels("N") fmt(0 3)) prefoot(\hline) starlevels(* 0.10 ** 0.05 *** 0.01) posthead(\hline) margin(p) nobaselevel
		
restore


*============================================
* 	LOGIT: 	round 1 of mixed groups (PHASE 1)
* ============================================
use $data
preserve
	keep if treat<5
	drop if type==0

**	Subjects in order=1 start with large and observe 0 Full C in fixed pairs
	replace fullc=0 if order==1

	keep if period==1 & groupsize>=12
	macro define controls "sex response_t wrong_ans"		

	collapse choice   fullc  $controls, by(treatment sessionID game order groupID ID)
	center response_t wrong_ans, inplace  standardize 
	gen game2=(game==2 | game==4)

*** LOGIT
	est clear	
	xtset ID game
	
	tab choice game if treat==1
	tab choice game if treat==2
	tab choice game if treat==3
		
	xtlogit choice i.treat i.fullc  i.order i.game2  $controls, re vce(cluster session)
	margins, dydx(*) at((base) _factor (asobs) $controls) post
	test 2.treatment=3.treatment
	test 2.treatment=4.treatment
	test 3.treatment=4.treatment
	eststo reg1

	estout *  using output2.tex,  replace cells("b(star fmt(3)) se(par fmt(3))") style(tex) label varlabels(1.order "Order 12-12-2-2"  1.game2 "$2^{nd}$ Supergame" duration "Duration" wrong_ans "Incorrect Answers" _cons Constant response_t "Response Time"   1.sex "Male"  sex "Male") collabels(,none) stats(N, labels("N") fmt(0 3)) prefoot(\hline) starlevels(* 0.10 ** 0.05 *** 0.01) posthead(\hline) margin(p) nobaselevel

restore


*==========================================================================================================
*	POWER TESTS --- COOPERATION  in MIXED GROUPS   (1 obs = 1 ECONOMY)
*	Minimum detectable effect size on cooperation. Total sample size = 32 x 2 mixed groups (control + treatment, balanced sample). 
*==========================================================================================================
use $data 
preserve
	keep if treat<5
	keep if groupsize==12

	collapse outcome profit, by (treatment sessionID groupID)
	table (treat) () , statistic(mean outcome)  statistic(sd outcome) nformat(%9.3f)
	table (treat) () , statistic(mean profit)  nformat(%9.3f)
	
	tabout treatment using output3.tex, replace sum c(mean outcome  sd outcome) f(3) style(tex) bt font(bold) h1(nil) h2(nil) h3(nil) lines(none)

*	Calculate the mean & N obs. for alternative hypothesis H1 (pre-treatment)
	tabstat outcome if treatment==1, stat(mean sd count) format(%9.3f) save
	matrix A=r(StatTotal)
	local mean_alt=A[1,1]
	local sd1_alt=A[2,1]

	** One sided tests
	** Converge
	power twomeans `mean_alt', sd1(`sd1_alt')  sd2(0.185)  power(0.8 0.9)  alpha(0.1) n(64) direction(upper) onesided
	** Diverge
	power twomeans `mean_alt', sd1(`sd1_alt')  sd2(0.176)  power(0.8 0.9)  alpha(0.1) n(64) direction(lower) onesided
	** Neutral+
	power twomeans `mean_alt', sd1(`sd1_alt')  sd2(0.192)  power(0.8 0.9)  alpha(0.1) n(64) direction(upper) onesided


	** Two sided tests
	** Converge
	power twomeans `mean_alt', sd1(`sd1_alt')  sd2(0.185)  power(0.8 0.9)  alpha(0.1) n(64) 
	** Diverge
	power twomeans `mean_alt', sd1(`sd1_alt')  sd2(0.176)  power(0.8 0.9)  alpha(0.1) n(64) 
	** Neutral+
	power twomeans `mean_alt', sd1(`sd1_alt')  sd2(0.192)  power(0.8 0.9)  alpha(0.1) n(64) 

restore


*=================================
* 	FIG 2: COOPERATION  (PHASE 1)
*=================================
use $data
preserve
	graph drop _all
	keep if treat<5
	keep if groupsize==12
	keep if type==1

	collapse choice, by(treatment sessionID country  groupID  game)
		bysort treat session groupID: egen mC=mean(choice)
	collapse  coop=choice   mC   (sd)  sdcoop=choice  (count) n=choice  ,   by(treatment country)

	table (treat) (country), statistic(mean  coop)  nformat(%9.3f) totals(treat)
	
	generate hi_sd = coop + invttail(n-1,0.025)*(sdcoop / sqrt(n)) 	
	generate low_sd = coop - invttail(n-1,0.025)*(sdcoop/ sqrt(n))

	gen position=treat
	replace position = 1 if treat==1
	replace position = 2.5 if treat==2
	replace position = 4 if treat==3
	replace position = 5.5 if treat==4
		
	replace position=position+0.35 if country==3
	replace position=position-0.35 if country==1

	gen avg=round(coop, 0.01)

	tw         (bar mC position if country==2, fcolor(gs14) lcolor(black))      (scatter coop position if country==1, sort connect(none) lpattern(solid) msymbol(T)  mfc(green) mlc(black) mlw(medthin)  msiz(medlarge)   mlab() mlabpos(12) mlabgap(5) mlabcolor (black) mlabsize(small))    (scatter coop position if country==2, sort connect(none)  lpattern(dash)  msymbol(O)      mfc(red) mlc(black) mlw(medthin)  msiz(medlarge)  mlab() mlabpos(12) mlabgap(5) mlabcolor (black) mlabsize(small))      (scatter coop position if country==3, sort msymbol(S)   connect(none)  lpattern(dash)  mfc(blue) mlc(black) mlw(medthin)  msiz(medlarge)  mlab() mlabpos(12) mlabgap(5) mlabcolor (black) mlabsize(small))                     (rcap low_sd hi_sd position, vertical bcolor(black) lwidth(medthin)) ,   xscale(range(0 5)) xlabels(1 "Neutral"  2.5 "Converge" 4 "Diverge"  5.5 "Neutral+" 6.5 " ", labs(medsmall) nogrid)    xtitle("", size(medsmall) )    yscale(range(0 1))  ylabels(0(0.2)1, labs(medsmall) nogrid)   ytitle(Cooperation Rate, size(medsmall))   scale(1.4) graphregion(style(none) color(gs16))    plotregion(margin(zero))   legend(order( 1 "Overall"  2 "Disadv."  3 "Middle"   4  "Advan.")  ring(0) pos(12) rows(1)  size(small) symxsize(*0.5))  	

	graph export Coop-Gap.eps, as(eps) preview(off) replace 

restore



*================================================
*	COOPERATION in MIXED GROUPS by TYPE (PHASE 1)		
*================================================
use $data 
preserve
	keep if treat<5
	keep if groupsize==12
	collapse choice, by(treatment sessionID country)
	
	local country  "1 2 3 "
	foreach i of local country {
		dis " "
		dis "********************************************** "
		dis "==       COOPERATION:  Country =" `i'  "     =="
		dis "********************************************** "
		ranksumex choice if country==`i' & inlist(treatment, 1,2), by(treatment)
		ranksumex choice if country==`i' & inlist(treatment, 1,3), by(treatment)
		ranksumex choice if country==`i' & inlist(treatment, 1,4), by(treatment)
		ranksumex choice if country==`i' & inlist(treatment, 2,3), by(treatment)
		ranksumex choice if country==`i' & inlist(treatment, 2,4), by(treatment)
		ranksumex choice if country==`i' & inlist(treatment, 3,4), by(treatment)
	}

	restore


*=========================================================================
*	COOPERATION  in MIXED GROUPS (PHASE 1 & PHASE 2) : 1 obs = 1 PLAYER TYPE
*=========================================================================
use $data 
preserve
	graph drop _all
	keep if treat<5
	keep if groupsize>=12
	keep if type==1
		
	macro define controls "sex duration response_t wrong_ans"		
	collapse outcome  $controls, by (treatment sessionID game groupID country order)
		center response_t wrong_ans duration, inplace  standardize 
		gen game2=(game==2 | game==4)
	
*** GLM regressions	
	est clear
	glm  outcome   i.treat    i.game2   i.order $controls if country==1 & game<5, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	test 2.treatment=3.treatment
	test 2.treatment=4.treatment
	test 3.treatment=4.treatment
	eststo reg1

	glm  outcome   i.treat    i.game2   i.order  $controls if country==2  & game<5, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	test 2.treatment=3.treatment
	test 2.treatment=4.treatment
	test 3.treatment=4.treatment
	eststo reg2

	glm  outcome   i.treat    i.game2   i.order  $controls if country==3  & game<5, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	test 2.treatment=3.treatment
	test 2.treatment=4.treatment
	test 3.treatment=4.treatment
	eststo reg3

	estout * using output4.tex,  replace cells("b(star fmt(3)) se(par fmt(3))") style(tex) label varlabels(1.order "Order 12-12-2-2"  1.game2 "$2^{nd}$ Supergame" duration "Duration" wrong_ans "Incorrect Answers" _cons Constant response_t "Response Time"   1.sex "Male"  sex "Male") collabels(,none) stats(N, labels("N") fmt(0 3)) prefoot(\hline) starlevels(* 0.10 ** 0.05 *** 0.01) posthead(\hline) margin(p) nobaselevel

	gen game5=(game==5)

	est clear
	glm  outcome   i.treat    i.game2   i.order i.game5 $controls if country==1, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	test 2.treatment=3.treatment
	test 2.treatment=4.treatment
	test 3.treatment=4.treatment
	eststo reg1

	glm  outcome   i.treat    i.game2   i.order  i.game5  $controls if country==2, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	test 2.treatment=3.treatment
	test 2.treatment=4.treatment
	test 3.treatment=4.treatment
	eststo reg2

	glm  outcome   i.treat    i.game2   i.order  i.game5  $controls if country==3, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	test 2.treatment=3.treatment
	test 2.treatment=4.treatment
	test 3.treatment=4.treatment
	eststo reg3

	estout * using output5.tex,  replace cells("b(star fmt(3)) se(par fmt(3))") style(tex) label varlabels(1.order "Order 12-12-2-2"  1.game2 "$2^{nd}$ Supergame" duration "Duration" wrong_ans "Incorrect Answers" _cons Constant response_t "Response Time"   1.sex "Male"  sex "Male") collabels(,none) stats(N, labels("N") fmt(0 3)) prefoot(\hline) starlevels(* 0.10 ** 0.05 *** 0.01) posthead(\hline) margin(p) nobaselevel


*** GLM regressions	
	est clear
	glm  outcome   i.country    i.game2   i.order $controls if treat==1 & game<5, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	test 2.country=3.country
	eststo reg11

	glm  outcome   i.country  i.game2   i.order  $controls if treat==2 & game<5, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	test 2.country=3.country
	eststo reg12

	glm  outcome   i.country  i.game2   i.order  $controls if treat==3 & game<5, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	test 2.country=3.country
	eststo reg13

	glm  outcome   i.country  i.game2   i.order  $controls if treat==4 & game<5, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	test 2.country=3.country
	eststo reg14

	estout * using output6.tex,  replace cells(b(star fmt(3)) se(par fmt(3))) style(tex) label varlabels(1.order "Order 12-12-2-2"  1.game2 "$2^{nd}$ Supergame" duration "Duration" wrong_ans "Incorrect Answers" _cons Constant response_t "Response Time"   1.sex "Male"  sex "Male") collabels(,none) stats(N, labels("N") fmt(0 3)) prefoot(\hline) starlevels(* 0.10 ** 0.05 *** 0.01) posthead(\hline) margin(p) nobaselevel


	est clear
	glm  outcome   i.country    i.game2   i.order  i.game5 $controls if treat==1, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	test 2.country=3.country
	eststo reg11

	glm  outcome   i.country  i.game2   i.order   i.game5 $controls if treat==2, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	test 2.country=3.country
	eststo reg12

	glm  outcome   i.country  i.game2   i.order   i.game5 $controls if treat==3, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	test 2.country=3.country
	eststo reg13

	glm  outcome   i.country  i.game2   i.order   i.game5 $controls if treat==4, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	test 2.country=3.country
	eststo reg14

	estout * using output7.tex,  replace cells(b(star fmt(3)) se(par fmt(3))) style(tex) label varlabels(1.order "Order 12-12-2-2"  1.game2 "$2^{nd}$ Supergame" duration "Duration" wrong_ans "Incorrect Answers" _cons Constant response_t "Response Time"   1.sex "Male"  sex "Male") collabels(,none) stats(N, labels("N") fmt(0 3)) prefoot(\hline) starlevels(* 0.10 ** 0.05 *** 0.01) posthead(\hline) margin(p) nobaselevel

restore



*======================================================================================
*	COOPERATION  in FIXED PAIRS (PHASE 1  & PHASE 2 separately)  	Unit of obs = 1 Economy 
*======================================================================================
use $data 
preserve
	graph drop _all
	keep if groupsize==2	
	keep if treat <5
	
	macro define controls "sex duration response_t wrong_ans"		
	collapse outcome   $controls, by (treatment sessionID game  groupID order )
	center response_t wrong_ans duration, inplace  standardize 
	gen game2=(game==2 | game==4)

*** GLM regression	
	est clear
	glm  outcome   i.treat   i.game2   i.order  $controls if game<5, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	test 2.treatment=3.treatment
	test 2.treatment=4.treatment
	test 3.treatment=4.treatment
	eststo reg1

	glm  outcome   i.treat    i.order $controls if game==5, cluster(sessionID) link(logit) family(binomial) robust nolog 
	margins, dydx(*) at((base) _factor (asobs) $controls ) post
	test 2.treatment=3.treatment
	test 2.treatment=4.treatment
	test 3.treatment=4.treatment
	eststo reg2

	estout * using output8.tex,  replace cells(b(star fmt(3)) se(par fmt(3))) style(tex) label varlabels(1.order "Order 12-12-2-2"  1.game2 "$2^{nd}$ Supergame" duration "Duration" wrong_ans "Incorrect Answers" _cons Constant response_t "Response Time"   1.sex "Male"  sex "Male") collabels(,none) stats(N, labels("N") fmt(0 3)) prefoot(\hline) starlevels(* 0.10 ** 0.05 *** 0.01) posthead(\hline) margin(p) nobaselevel
		
restore


*===================================================
* 	INTEGRATION GAINS  in PHASE 1  (1 Obs = 1 subject ID)
*===================================================
use $data
preserve
	keep if game<5

	collapse choice profit, by(treatment groupsize ID)		
	reshape wide choice profit, i(ID) j(groupsize)
	gen temp=profit12-profit2
	table (treat) (), statistic(mean  temp)  nformat(%9.3f)
	
local variable "1 2 3 4 5"
	foreach i of local variable{
	dis "=== PROFITS in Fixed Pairs vs. Mixed Groups (Phase 1):   Treat = `i' ==="
	signrank profit2=profit12 if treatment==`i'
	}
	
restore	



*============================================================================================================
*	PAYOFFS : 	DO MIXED GROUPS support HIGHER AVERAGE PAYOFFS than PARTNERSHIPS? (Regression ON GAMES 1-4 )
*============================================================================================================
	use $data
	preserve
	keep if treat<5
	drop if game==5

	macro define controls "sex duration duration_lagged response_t wrong_ans"		
	collapse profit $controls, by(treatment sessionID game groupID groupsize order)
	
	gen secondGame=(game==2 | game==4  )
	center response_t wrong_ans duration duration_lagged, inplace  standardize 

	macro define controls "sex duration duration_lagged response_t wrong_ans"		

	est clear
	reg  profit   ib2.groupsize i.order i.secondGame $controls if treat==1,  cluster(sessionID)
	eststo reg1

	reg  profit   ib2.groupsize i.order i.secondGame $controls if treat==2,  cluster(sessionID)
	eststo reg2
	
	reg  profit   ib2.groupsize i.order i.secondGame $controls if treat==3,  cluster(sessionID)
	eststo reg3

	reg  profit   ib2.groupsize i.order i.secondGame $controls if treat==4,  cluster(sessionID)
	eststo reg4

	estout *  using output9.tex,  replace cells(b(star fmt(3)) se(par fmt(3))) style(tex) label varlabels(game Supergame  duration "Supergame duration" wrong_ans "Incorrect Answers" _cons Constant male Male response_t "Response Time" 1.order "12-12-2-2"   1.sex "Male"     1.secondGame "$2^{nd}$ game") collabels(,none) stats(N r2 r2_a  , labels("N" "R$^2$" "adj R$^2$" ) fmt(0 3)) prefoot(\hline) starlevels(* 0.10 ** 0.05 *** 0.01) posthead(\hline) margin(p) nobaselevel	

	restore



*==========================================================
* 	TABLE 3 :   INTEGRATION GAINS in PHASE 1    +  Stat tests  (1 Obs = 1 type in a session)
*==========================================================
use $data
preserve
	keep if  game<5

	collapse profit, by(treatment session groupsize country)		
	reshape wide profit, i(treatment session country) j(groupsize)
	gen temp=profit12-profit2
	table (treat) (country), statistic(mean  temp)  nformat(%9.3f) totals(treat)
	
local variable "1 2 3 4 "
	foreach i of local variable{
	dis "=== DISADV Treat = `i' ==="
	signrank profit2=profit12 if treatment==`i' & country==1
	dis "=== MIDDLE   - Treat = `i' ==="
	signrank profit2=profit12 if treatment==`i' & country==2
	dis "=== DISADVANT - Treat = `i' ==="
	signrank profit2=profit12 if treatment==`i' & country==3
	}
	
restore	


*==========================================================
*		FIGURE  -- 	DISTRIBUTION OF VOTES 	(1 obs = 1 session)
*==========================================================
use $data 
preserve
	drop vote
	keep if treat<5
		
	collapse vote*, by(treatment sessionID)
	count if treat==1

	collapse vote1 vote2 vote3 (sem) sem1=vote1  sem2=vote2  sem3=vote3, by(treatment)
	reshape long vote sem, i(treatment) j(voteNEW)
				
	gen hi_sem=vote + sem
	gen low_sem=vote - sem
			
	gen position = voteNEW if treatment==3
	replace position = voteNEW+4 if treatment==1
	replace position = voteNEW+8 if treatment==2
	replace position = voteNEW+12 if treatment==4
		replace position = position -0.25 if voteNEW==2
		replace position = position - 0.5 if voteNEW==3
	
	sort position
	list position treatment voteNEW, sepby(treatment)	
	
	gen avg=round(vote, 0.01)
				
	graph twoway  	(bar vote position if voteNEW==1,  fcolor(gs4) lcolor(black)) 	 (bar vote position if voteNEW==2, fcolor(gs12) lcolor(black))   	(bar vote position if voteNEW==3, fcolor(gs16) lcolor(black))  	     (rcap hi_sem low_sem position, bcolor(black) lw(thin)), 	 xlabel(1.75 "Diverge"  5.75 "Neutral"  9.75  "Converge"  13.75  "Neutral+", labsize(medsmall) )    	 xtitle("") ytitle("Relative frequency", size(medsmall))  	            	yscale(range(1.00)) ylabel(0(0.2)1, labsize(medsmall) nogrid)       	legend(order(1 "Leave" 2 "Exclude" 3 "Stay")  size(medsmall) region(lcolor(white))   ring(0) pos(10) rows(1)   symxsize(*0.4))   graphregion(color(white)fcolor(white))   plotregion(margin(zero)) scale(1.4) 

	graph export votes.eps, as(eps) preview(off) replace
	
	table (treat) (voteNEW), statistic(mean  avg)  nformat(%9.3f) nototals

restore



*============================================================================================
*	STATISTICAL ANALYSIS  ---  VOTING within a TREATMENT  at Session level  (8 independent observations)
*============================================================================================
	use $data 
	preserve
	
*	Use only the last game (since votes are cast in game 4, but reported in all games in the dataset)
	drop if game!=5
	drop vote
	label drop countryl
	
** 	Collapse by SESSION  (vote1=Leave, vote2=Exclude, vote3=Stay)
	collapse vote1 vote2 vote3, by(treatment sessionID)
	count if treat==1
	
** 1 obs = 1 session. SIGNRANK TEST assume symmetry of diferences, which "signtest" does not.
	local variable "1 2 3 4 5"
	foreach i of local variable{
	dis "===Treat = `i' ==="

	dis "=== Leave vs. Exclude ==="
	signtest  vote1=vote2 if treat==`i' 

	dis "=== Exclude vs. Stay ==="
	signtest  vote2=vote3 if treat==`i' 
	
	dis "=== Leave vs. Stay ==="
	signtest  vote1=vote3 if treat==`i' 
		}
	
**	Is the difference (Vote 2- Vote3)=(Exclude - Stay) equal across treatments?
	gen temp=vote2-vote3

	reshape long vote,   i(treatment sessionID)    j(group_preferences)
	table (group_) (treat) , statistic(mean  vote)  nformat(%9.2f) nototals

	replace temp=. if group_preferences!=3
	ranksum temp if inlist(treat, 1,2), by(treat) exact
	ranksum temp if inlist(treat, 2,3), by(treat) exact
	ranksum temp if inlist(treat, 1,3), by(treat) exact
	ranksum temp if inlist(treat, 1,5), by(treat) exact

**   Is there a difference in voting choices ACROSS TREATMENTS ? 
	local voting "1 2 3"
	foreach i of local voting{
	dis "==       Vote " `i'  "         =="
	 ranksumex vote if group_preferences==`i' & inlist(treat, 1,2), by(treat) 	
	 ranksumex vote if group_preferences==`i' & inlist(treat, 1,3), by(treat) 
 	 ranksumex vote if group_preferences==`i' & inlist(treat, 1,4), by(treat) 
	 ranksumex vote if group_preferences==`i' & inlist(treat, 2,3), by(treat) 
  	 ranksumex vote if group_preferences==`i' & inlist(treat, 1,5), by(treat) 
	}
restore


*========================================================================
*	MUTINOMIAL LOGIT (individual level) -  TREATMENT EFFECT ON GROUP CHOICES  (TABLE 4)
*========================================================================
use $data 
preserve
	keep if treat<5
	drop if game==5
	macro define controls "sex response_t wrong_ans"		

	collapse  choice otherchoice profit fullc outcome  vote* $controls , by (treatment sessionID game country groupsize ID order)

**	Realized profit of average supergame (Fixed Pairs and Mixed Groups)
	gen profitP=profit if groupsize==2
	gen profitMG=profit if groupsize==12

 **	COLLAPSE at SESSION level by ID:     [voteNEW=1,2,3=leave,exclude,stay ]
	collapse  fullc profitP profitMG  vote1 vote2 vote3 voteNEW  $controls, by (treatment sessionID  country ID order)

	table (country) (treat) , statistic(mean  vote1)  nformat(%9.2f) nototals
	table (country) (treat) , statistic(mean  vote2)  nformat(%9.2f) nototals
	table (country) (treat) , statistic(mean  vote3)  nformat(%9.2f) nototals
	

**	MULTINOMIAL LOGIT 
	est clear 
	gen Gains=profitMG - profitP
	macro define controls "response_t   wrong_ans  i.sex"	 
	center  response_t wrong_ans  Gains, inplace  standardize 
	
	mlogit voteNEW    ib1.treat##c.Gains   i.order  $controls, cluster(session)
	eststo reg1
 
** Marginal effects by choice
	local i "1 2 3"
	foreach i in 1 2 3 {
	quietly margins, dydx(*) at((means) _all (base) _factor) predict(outcome(`i')) post
	eststo Choice`i'
	estimates restore reg1
	}

	estout Choice1 Choice2 Choice3 using output10.tex, replace cells(b(star fmt(3)) se(par fmt(3))) style(tex)  stats(N, labels("N")  fmt(0 3)) prefoot(\hline) starlevels(* 0.10 ** 0.05 *** 0.01) posthead(\hline) varlabels(2.treatment "\ \ Converge" 3.treatment "\ \ Diverge" 4.treatment "\ \ Neutral+" 1.fullc "Full Cooperation=1" 2.fullc "Full Cooperation=2"  1.order "Order (12-12-2-2)" Gains "Realized Gain"    game Supergame  wrong_ans "Incorrect Answers" _cons Constant male Male response_t "Response Time" 1.sex "Male" ) 

	eststo drop Choice1 Choice2 Choice3

	estimates restore reg1
	margins, dydx(*) at((means) _all (base) _factor) post
		
** ARE THERE DIFFERENT VOTING OUTCOMES ACROSS TREATMENTS?
	local vote "1 2 3"
	foreach i of local vote {
	dis "*******       Vote =" `i'  "     ******* "
	test [2.treatment]`i'._predict =[3.treatment]`i'._predict 
	test [2.treatment]`i'._predict =[4.treatment]`i'._predict 
	test [3.treatment]`i'._predict =[4.treatment]`i'._predict 
	}
	
	test [c.Gains]2._predict =[c.Gains]3._predict 

restore

** == CHAT VS. NEUTRAL ==**
use $data 
preserve
	keep if inlist(treat,1,5)
	keep if order==0
	drop if game==5
	macro define controls "sex response_t wrong_ans"		

	collapse  choice otherchoice profit fullc outcome  vote* $controls , by (treatment sessionID game country groupsize ID)

	gen profitP=profit if groupsize==2
	gen profitMG=profit if groupsize==12

 **	COLLAPSE at SESSION level by ID:     [voteNEW=1,2,3=leave,exclude,stay ]
	collapse  fullc profitP profitMG  vote1 vote2 vote3 voteNEW  $controls, by (treatment sessionID  country ID)

	table (country) (treat) , statistic(mean  vote1)  nformat(%9.2f) nototals
	table (country) (treat) , statistic(mean  vote2)  nformat(%9.2f) nototals
	table (country) (treat) , statistic(mean  vote3)  nformat(%9.2f) nototals

**	MULTINOMIAL LOGIT 
	est clear 
	gen Gains=profitMG - profitP
	macro define controls "response_t   wrong_ans  i.sex"	 
	center  response_t wrong_ans  Gains, inplace  standardize 
	
	mlogit voteNEW    ib1.treat##c.Gains   $controls, cluster(session)
	eststo reg1
 
** Marginal effects by choice
	local i "1 2 3"
	foreach i in 1 2 3 {
	quietly margins, dydx(*) at((means) _all (base) _factor) predict(outcome(`i')) post
	eststo Choice`i'
	estimates restore reg1
	}

	estout Choice1 Choice2 Choice3 using output11.tex, replace cells(b(star fmt(3)) se(par fmt(3))) style(tex)  stats(N, labels("N")  fmt(0 3)) prefoot(\hline) starlevels(* 0.10 ** 0.05 *** 0.01) posthead(\hline) varlabels(5.treatment "Neutral-Chat"   Gains "Realized Gain"    game Supergame  wrong_ans "Incorrect Answers" _cons Constant male Male response_t "Response Time" 1.sex "Male" ) 

	eststo drop Choice1 Choice2 Choice3

restore



*============================================================================================================
*	MUTINOMIAL LOGIT (individual level)-  GROUP CHOICES (ALL treatments pooled)
*============================================================================================================
use $data 
preserve
	graph drop _all
	drop if game==5
	keep if treat<5
	macro define controls "sex response_t wrong_ans"		

 *** COLLAPSE by ID in SUPERGAME
	collapse  choice otherchoice profit outcome vote* $controls , by (treatment sessionID game groupsize ID order)

	gen profitP=profit if groupsize ==2
	gen profitMG=profit if groupsize ==12

*	Cooperation choices and relative to opponents
	gen choiceP=choice if groupsize ==2
	gen choiceMG=choice if groupsize ==12
	gen otherchoiceP=otherchoice if groupsize ==2
	gen otherchoiceMG=otherchoice if groupsize ==12
	gen RelCoop_P=choiceP-otherchoiceP
	gen RelCoop_MG=choiceMG-otherchoiceMG

*	COLLAPSE by ID/ SESSION:     [voteNEW=1,2,3=leave,exclude,stay ]
	collapse  profitP profitMG  RelCoop_P RelCoop_MG vote1 vote2 vote3 voteNEW  $controls, by (treatment sessionID  ID order)
	gen Gains=profitMG - profitP
	center  response_t wrong_ans  Gains, inplace  standardize 

*	Create categories of cooperation 1=Free-rider   2=Conditional Cooperator 3= Altruist
	egen RC_FP = cut(RelCoop_P), at(-1, -0.33, 0.33,1.1) icodes
	egen RC_MG = cut(RelCoop_MG), at(-1, -0.33, 0.33,1.1)  icodes
	replace RC_FP=RC_FP+1
	replace RC_MG=RC_MG+1	
	tabstat RelCoop_P, by(RC_FP) stats(min max)
	tabstat RelCoop_MG, by(RC_MG) stats(min max)
	
**	MULTINOMIAL LOGIT
	macro define controls "response_t  wrong_ans   i.sex"	 
	est clear 
	mlogit voteNEW    ib1.treat  ib2.RC_MG  ib2.RC_FP i.order  $controls, cluster(session)
	eststo reg1

** Marginal effects by choice
	local i "1 2 3"
	foreach i in 1 2 3 {
	quietly margins, dydx(*) at((means) _all (base) _factor) predict(outcome(`i')) post
	eststo Choice`i'
	estimates restore reg1
	}

	estout Choice1 Choice2 Choice3 using output12.tex, replace cells(b(star fmt(3)) se(par fmt(3))) style(tex)  stats(N, labels("N")  fmt(0 3)) prefoot(\hline) starlevels(* 0.10 ** 0.05 *** 0.01) posthead(\hline) varlabels(1.RC_FP "\ \ Low (Fixed Pairs)"  3.RC_FP "\ \ High  (Fixed Pairs)"  1.RC_MG "\ \ Low (Mixed Groups)"  3.RC_MG "\ \ High (Mixed Groups)"  1.order "Order 12-12-2-2" Gains "Realized Gains"    game Supergame  wrong_ans "Incorrect Answers" _cons Constant male Male response_t "Response Time" 1.sex "Male" ) 

restore



*=============================================================
*	MULTINOMIAL LOGIT (individual level)-  GROUP CHOICES for a TYPE 
*=============================================================
use $data 
preserve
	drop if game==5

	*** CHOOSE the treatment (do ti for all treatments)
	keep if treat==1
	macro define controls "sex response_t wrong_ans"		

	collapse  choice otherchoice profit outcome vote* $controls , by (treatment sessionID game country groupsize ID order)

	gen profitP=profit if groupsize ==2
	gen profitMG=profit if groupsize ==12

*	COLLAPSE by ID/ SESSION:     [voteNEW=1,2,3=leave,exclude,stay ]
	collapse  profitP profitMG  vote1 vote2 vote3 voteNEW  $controls, by (treatment sessionID  country ID order)
	gen Gains=profitMG - profitP
	center  response_t wrong_ans  Gains, inplace  standardize 
		
**	MULTINOMIAL LOGIT
	macro define controls "response_t  wrong_ans   i.sex"	 
	est clear 
	mlogit voteNEW    ib1.country   c.Gains  i.order    $controls, cluster(session)
	eststo reg1

** Marginal effects by choice
	local i "1 2 3"
	foreach i in 1 2 3 {
	quietly margins, dydx(*) at((means) _all (base) _factor) predict(outcome(`i')) post
	eststo Choice`i'
	estimates restore reg1
	}

	estout Choice1 Choice2 Choice3 using output13.tex, replace cells(b(star fmt(3)) se(par fmt(3))) style(tex)  stats(N, labels("N")  fmt(0 3)) prefoot(\hline) starlevels(* 0.10 ** 0.05 *** 0.01) posthead(\hline) varlabels(2.country "Middle" 3.country "Advantaged" 1.order "Order 12-12-2-2" Gains "Realized Gains"    game Supergame  wrong_ans "Incorrect Answers" _cons Constant male Male response_t "Response Time" 1.sex "Male" ) 

	eststo drop Choice1 Choice2 Choice3

	estimates restore reg1
	margins, dydx(*) at((means) _all (base) _factor) post
		
** ARE THERE DIFFERENT VOTING OUTCOMES ACROSS COUNTRIES 2 AND 3?
	test [2.country]1._predict =[3.country]1._predict 
	test [2.country]2._predict =[3.country]2._predict 
	test [2.country]3._predict =[3.country]3._predict 

restore

	
**== NEUTRAL-CHAT only (no order variable here) ==**
use $data 
preserve
	drop if game==5
	keep if treat==5
	macro define controls "sex response_t wrong_ans"		

 *** COLLAPSE by ID in SUPERGAME
	collapse  choice otherchoice profit outcome vote* $controls , by (sessionID game country groupsize ID)

	gen profitP=profit if groupsize ==2
	gen profitMG=profit if groupsize ==12

*	COLLAPSE by ID/ SESSION:     [voteNEW=1,2,3=leave,exclude,stay ]
	collapse  profitP profitMG  vote1 vote2 vote3 voteNEW  $controls, by (sessionID  country ID)
	gen Gains=profitMG - profitP
	center  response_t wrong_ans  Gains, inplace  standardize 
		
**	MULTINOMIAL LOGIT
	macro define controls "response_t  wrong_ans   i.sex"	 
	est clear 
	mlogit voteNEW    ib1.country   c.Gains   $controls, cluster(session)
	eststo reg1

** Marginal effects by choice
	local i "1 2 3"
	foreach i in 1 2 3 {
	quietly margins, dydx(*) at((means) _all (base) _factor) predict(outcome(`i')) post
	eststo Choice`i'
	estimates restore reg1
	}

	estout Choice1 Choice2 Choice3 using output14.tex, replace cells(b(star fmt(3)) se(par fmt(3))) style(tex)  stats(N, labels("N")  fmt(0 3)) prefoot(\hline) starlevels(* 0.10 ** 0.05 *** 0.01) posthead(\hline) varlabels(2.country "Middle" 3.country "Advantaged" Gains "Realized Gains"    game Supergame  wrong_ans "Incorrect Answers" _cons Constant male Male response_t "Response Time" 1.sex "Male" ) 

	eststo drop Choice1 Choice2 Choice3

	estimates restore reg1
	margins, dydx(*) at((means) _all (base) _factor) post
		
** ARE THERE DIFFERENT VOTING OUTCOMES ACROSS TYPES 2 AND 3?
	test [2.country]1._predict =[3.country]1._predict 
	test [2.country]2._predict =[3.country]2._predict 
	test [2.country]3._predict =[3.country]3._predict 

restore

	
	
*==============================================================
* 		DISTRIBUTION OF VOTES by TYPE (1 obs = 1 type in a  session)
*==============================================================
	use $data
	preserve
		keep if game==5 & period==1
	
		by treat, sort: groups(vote)
	
		collapse vote1 vote2 vote3, by(treatment sessionID country)
			
		table (country) (treat), statistic(mean  vote1)  statistic(sd vote1)  nformat(%9.2f) nototals
		table (country) (treat) , statistic(mean  vote2)  statistic(sd vote2)  nformat(%9.2f) nototals
		table (country) (treat) , statistic(mean  vote3)  statistic(sd vote3)  nformat(%9.2f) nototals
	restore
	


*=====================================
* 	TABLE: GROUP CHOICES & MAJORITIES
*=====================================
*	How many groups of size 24 were formed in game 5?
	preserve
		collapse ymd if game==5,  by(treat sessionID groupsize)
		groups(groupsize)
		tab treat groupsize
	restore	

* N. of obs. for 2-player groups, 12-player groups, 16-player group and 24-player groups.
	bysort groupsize: distinct groupID if game<5 
	bysort groupsize: distinct groupID if game==5
	
*	How many votes did each choice get? Calculate what was the majority vote (by country and treat)
	local votes "1 2 3"
		foreach i of local votes {
		by treat sessionID country (ID), sort: egen temp`i'=total(voteNEW==`i') if game==1 & period==1
		by treat sessionID country (game period), sort: gen temp=temp`i'[1]
		replace temp`i'=temp
		drop temp
		}
	
	sort treat sessionID game period country  ID 

* Find the majority vote (8 players per country), consider there can be ties (e.g, 2 3 3   or 4 0 4)
* Majority vote in a country (1=leave, 2=exclude, 3=stay, 12=equal 1 & 2 votes, etc.)
	by treat sessionID country, sort: gen majority=1 if temp1==max(temp1,temp2, temp3) & temp1!=temp2 & temp1!=temp3
	by treat sessionID country, sort: replace majority=2 if temp2==max(temp1,temp2, temp3) & temp2!=temp1 & temp2!=temp3
	by treat sessionID country, sort: replace majority=3 if temp3==max(temp1,temp2, temp3) & temp3!=temp1 & temp3!=temp2
	by treat sessionID country, sort: replace majority=12 if majority==. & temp1==temp2
	by treat sessionID country, sort: replace majority=13 if majority==. & temp1==temp3
	by treat sessionID country, sort: replace majority=23 if majority==. & temp2==temp3
drop temp*
